//
//  QSHud.swift
//  TestSwift
//
//  Created by Song on 2018/5/3.
//  Copyright © 2018年 Song. All rights reserved.
//

import UIKit

class QSHud: NSObject {
    /// 配置项
    // 遮罩颜色
    private var qs_maskLayerColor: UIColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
    // 吐司背景颜色
    private var qs_toastViewColor: UIColor = UIColor.white
    // 吐司的圆角
    private var qs_toastViewRadius: CGFloat = 10.0
    // title字体颜色
    private var qs_titleColor: UIColor = UIColor.black
    // title字体大小
    private var qs_titleFont: UIFont = UIFont.systemFont(ofSize: 17.0)
    // 吐司消失时间
    private var qs_dismissInterval: TimeInterval = 2.5
    // 加载中图片
    private var qs_loadingImgName: String = "universal_loading"
    // 成功图片
    private var qs_successImgName: String = "icon_success"
    // 失败图片
    private var qs_errorImgName: String = "icon_false"
    
    // 消失完成时的回调
    private var hudDismissComplete: (() -> ())?
    private var dismissTask: QSTask?
    
    // hudView
    private var hudView: QSHudView?
    
    // 单例
    static var shared : QSHud {
        struct Static {
            static let instance : QSHud = QSHud()
        }
        return Static.instance
    }
    
    /// 设置配置项
    ///
    /// - Parameters:
    ///   - maskLayerColor: 遮罩层颜色
    ///   - toastViewColor: 吐司颜色
    ///   - toastViewRadius: 吐司圆角
    ///   - titleColor: title颜色
    ///   - titleFont: title字体
    ///   - loadingImg: 加载中图片
    ///   - successImg: 成功图片
    ///   - errorImg: 失败图片
    ///   - dismissInterval: 多久消失
    class func qs_setConfigurationItems(maskLayerColor: UIColor? = nil, toastViewColor: UIColor? = nil, toastViewRadius: CGFloat? = nil, titleColor: UIColor? = nil, titleFont: UIFont? = nil, loadingImg: String? = nil, successImg: String? = nil, errorImg: String? = nil, dismissInterval: TimeInterval? = nil) {
        if maskLayerColor != nil {
            self.shared.qs_maskLayerColor = maskLayerColor!
        }
        
        if toastViewColor != nil {
            self.shared.qs_toastViewColor = toastViewColor!
        }
        
        if toastViewRadius != nil {
            self.shared.qs_toastViewRadius = toastViewRadius!
        }
        
        if titleColor != nil {
            self.shared.qs_titleColor = titleColor!
        }
        
        if titleFont != nil {
            self.shared.qs_titleFont = titleFont!
        }
        
        if loadingImg != nil {
            self.shared.qs_loadingImgName = loadingImg!
        }
        
        if successImg != nil {
            self.shared.qs_successImgName = successImg!
        }
        
        if loadingImg != nil {
            self.shared.qs_errorImgName = errorImg!
        }
        
        if dismissInterval != nil {
            self.shared.qs_dismissInterval = dismissInterval!
        }
    }
    
    /// 加载中
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - toView: 吐司加到哪个view上，nil加到window
    ///   - isNeedMaskLayer: 是否需要遮罩
    ///   - dismissComplete: 消失后回调
    class func qs_showProgress(title: String? = nil, toView: UIView? = nil, isNeedMaskLayer: Bool = true, dismissComplete: (() -> ())? = nil) {
        var theView = UIView.init()
        if toView == nil {
            theView = UIApplication.shared.keyWindow!
        } else {
            theView = toView!
        }
        
        self.qs_addHudView(toView: theView, img: QSHud.shared.qs_loadingImgName, title: title, isNeedMaskLayer: isNeedMaskLayer, isImgRotate: true, dismissComplete: dismissComplete)
    }
    
    /// 成功
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - toView: 吐司加到哪个view上，nil加到window
    ///   - isNeedMaskLayer: 是否需要遮罩
    ///   - dismissInterval: 消失时间，默认2.5秒
    ///   - dismissComplete: 消失后回调
    class func qs_showSuccess(title: String? = nil, toView: UIView? = nil, isNeedMaskLayer: Bool = true, dismissInterval: TimeInterval = 2.5, dismissComplete: (() -> ())? = nil) {
        var theView = UIView.init()
        if toView == nil {
            theView = UIApplication.shared.keyWindow!
        } else {
            theView = toView!
        }
        
        self.qs_addHudView(toView: theView, img: QSHud.shared.qs_successImgName, title: title, isNeedMaskLayer: isNeedMaskLayer, isImgRotate: false, dismissComplete: dismissComplete)
        
        // 自动消失
        if dismissInterval > 0.0 {
            QSHud.shared.dismissTask = QSDispatch.qs_delay(dismissInterval) {
                QSHud.qs_dismiss()
            }
        }
    }
    
    /// 失败
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - toView: 吐司加到哪个view上，nil加到window
    ///   - isNeedMaskLayer: 是否需要遮罩
    ///   - dismissInterval: 消失时间，默认2.5秒
    ///   - dismissComplete: 消失后回调
    class func qs_showError(title: String? = nil, toView: UIView? = nil, isNeedMaskLayer: Bool = true, dismissInterval: TimeInterval = 2.5, dismissComplete: (() -> ())? = nil) {
        var theView = UIView.init()
        if toView == nil {
            theView = UIApplication.shared.keyWindow!
        } else {
            theView = toView!
        }
        
        self.qs_addHudView(toView: theView, img: QSHud.shared.qs_errorImgName, title: title, isNeedMaskLayer: isNeedMaskLayer, isImgRotate: false, dismissComplete: dismissComplete)
        
        // 自动消失
        if dismissInterval > 0.0 {
            QSHud.shared.dismissTask = QSDispatch.qs_delay(dismissInterval) {
                QSHud.qs_dismiss()
            }
        }
    }
    
    /// 文字
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - toView: 吐司加到哪个view上，nil加到window
    ///   - isNeedMaskLayer: 是否需要遮罩
    ///   - dismissInterval: 消失时间，默认2.5秒
    ///   - dismissComplete: 消失后回调
    class func qs_showText(title: String, toView: UIView? = nil, isNeedMaskLayer: Bool = true, dismissInterval: TimeInterval = 2.5, dismissComplete: (() -> ())? = nil) {
        var theView = UIView.init()
        if toView == nil {
            theView = UIApplication.shared.keyWindow!
        } else {
            theView = toView!
        }
        
        self.qs_addHudView(toView: theView, img: nil, title: title, isNeedMaskLayer: isNeedMaskLayer, isImgRotate: false, dismissComplete: dismissComplete)
        
        // 自动消失
        if dismissInterval > 0.0 {
            QSHud.shared.dismissTask = QSDispatch.qs_delay(dismissInterval) {
                QSHud.qs_dismiss()
            }
        }
    }
    
    /// 消失
    class func qs_dismiss() {
        if QSHud.shared.dismissTask != nil {
            QSDispatch.qs_cancle(QSHud.shared.dismissTask)
        }
        
        if let hudView = QSHud.shared.hudView {
            UIView.animate(withDuration: 0.35, animations: {
                hudView.alpha = 0.0
            }) { (complete) in
                if QSHud.shared.hudDismissComplete != nil {
                    QSHud.shared.hudDismissComplete!()
                }
                
                hudView.removeFromSuperview()
            }
        }
    }
    
    // MARK: - Private Methods
    private class func qs_addHudView(toView: UIView? = nil, img: String? = nil, title: String? = nil, isNeedMaskLayer: Bool, isImgRotate: Bool, dismissComplete: (() -> ())? = nil) {
        let hud = QSHud.shared
        
        if hud.hudView != nil {
            self.qs_dismiss()
        }
        
        hud.hudView = QSHudView.init(toastViewColor: hud.qs_toastViewColor, toastViewRadius: hud.qs_toastViewRadius, img: img, title: title, titleFont: hud.qs_titleFont, titleColor: hud.qs_titleColor, isImgRotate: isImgRotate, isNeedMaskLayer: isNeedMaskLayer, complete: dismissComplete)
        hud.hudView?.alpha = 0.0
        
        toView?.addSubview(hud.hudView!)
        hud.hudView?.snp.makeConstraints { (make) in
            if isNeedMaskLayer {
                make.edges.equalTo(UIEdgeInsets.zero)
            } else {
                make.center.equalTo(toView!)
                make.top.left.greaterThanOrEqualTo(50.0)
                make.right.bottom.lessThanOrEqualTo(-50.0)
            }
        }
        
        if isNeedMaskLayer {
            hud.hudView?.backgroundColor = hud.qs_maskLayerColor
        }
        
        // 消失回调
        if let complete = dismissComplete {
            hud.hudDismissComplete = complete
        }
        
        // 显示动画
        hud.qs_showHudView(hud.hudView!)
    }
    
    /// 显示
    private func qs_showHudView(_ hudView: UIView) {
        UIView.animate(withDuration: 0.35, animations: {
            hudView.alpha = 1.0
        })
    }
}
